package com.why.blog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.why.blog.bean.Type;
import com.why.blog.service.BlogService;
import com.why.blog.service.typeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: blog
 * @description:
 * @author: @why
 * @create: 2021-01-19 23:08
 **/

@Controller
@RequestMapping("/admin")
@Slf4j
public class adminMyTypeController {

    @Autowired
    private typeService type;
    @Autowired
    private BlogService blog;

    /**
     * 跳转到分类管理页面
     *
     * @param PageNo
     * @param model
     * @return
     */

    @RequestMapping("/type-mgr")
    public String typeMsg(@RequestParam(name = "pageNum",defaultValue = "1") Integer PageNo, Model model) {
        PageNo = PageNo >= 1 ? PageNo : 1;
        PageHelper.startPage(PageNo, 6);
        PageInfo<Type> typePageInfo = new PageInfo<>(type.getListType());
        model.addAttribute("types", typePageInfo);

        return "admin/type-mgr";

    }

    /**
     * 分类更新,重点，方法必须右返回值！
     * @param type
     */
    @ResponseBody
    @RequestMapping("/typeUpdate")
    public String typeUpdate(Type type) {

        //System.out.println(type);
        this.type.updateType(type);
        return "1";

    }

    /**
     * 分类删除,同时删除分类对应的博客！
     *  mybatis中一个标签执行多条语句，加入 allowMultiQueries=true 即可
     *
     * @param id
     */
    @ResponseBody
    @RequestMapping("/typeDelete")
    public String typeUpdate(Integer  id) {

      type.deleteTypeById(id);
       log.warn("删除成功");
        return "666";
    }

    /**
     * 添加分类
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/addType")
    public String addType(Type  type) {
        if(!StringUtils.hasLength(type.getName()))
        {
            return "3";
        }
        Integer typeByName = this.type.getTypeByName(type.getName());
        if(typeByName!=null){
            return "2";
        }
        else{
            this.type.saveType(type);
            log.warn("添加成功");
            return "1";
        }


    }


}
